# -*- coding: utf-8 -*-

import  os
from getOsType import  GetOsTypeCmd

class Terminal():
    def __init__(self):
        if GetOsTypeCmd.isWindows():
            self.Terminal = WindowsTerminal()
        elif GetOsTypeCmd.isLinux():
            self.Terminal = LinuxTerminal()

        pass

    def openTerminal(self):
        self.Terminal.openTerminal()
        pass

    def openTerminalWithArgs(self,path=os.getcwd()):
        self.Terminal.openTerminalWithArgs(path)
        pass



class WindowsTerminal():
    def __init__(self):
        pass

    def openTerminal(self):
        os.system("start cmd.exe")
        pass

    def openTerminalWithArgs(self, path_=os.getcwd()):
        path = os.path.abspath(path_)
        partition = path.split(os.sep)[0]

        os.system("start cmd.exe /k %s && %s" % (partition,"cd %s" % path))

        pass


class LinuxTerminal():
    def __init__(self):
        pass

    def openTerminal(self):
        # ubuntu 22.04 测试
        cmd = 'gnome-terminal -- bash -c "cd;exec bash"'
        os.system(cmd)
        pass

    def openTerminalWithArgs(self, path=os.getcwd(),isNewWindow = 1):
        if isNewWindow:
            return self.openTerminalWithArgsInNewWindow(path)
        else:
            return self.openTerminalWithArgsInNewTable(path)
        pass

    def openTerminalWithArgsInNewWindow(self,path):
        cmd = 'gnome-terminal --window -- bash -c "cd %s ;exec bash"' % path
        os.system(cmd)
        pass

    def openTerminalWithArgsInNewTable(self,path):
        cmd = 'gnome-terminal --tab -- bash -c "cd %s ;exec bash"' % path
        os.system(cmd)
        pass

TerminalCmd = Terminal()
if __name__ == "__main__":
    TerminalCmd.openTerminalWithArgs()